<?php

class DichvuController extends MyController
{
	function init()
	{
		parent::init();
	}
	
	function actionIndex()
	{	
		$this->redirect(array($this->controller_id.'/view'));
	}	
	
	function actionView($parent_id = 0, $page = 1)
	{
		$pages = new CPagination;
		$pages->pageSize = $this->website->beperpage;
		
		$rows = NULL;
		
		
		$rows = dichvu::model()->findAll(array(
			"order" => "`order` ASC",
			"offset" => ($page - 1) * $this->website->beperpage,
			"limit" => $this->website->beperpage
		));
		
		$pages->itemCount = dichvu::model()->count();
		
		$this->data['rows']= $rows;
		$this->data['pages'] = $pages;
		$this->data['page']= $page;
		$this->render('view', $this->data);
	}
	
	function actionAdd($page = 1)
	{
		$this->action = "Thêm mới";
		$row = new dichvu;
		$row->id = 0;
		$row->order = Yii::app()->db->createCommand(
			"SELECT max(`order`) FROM dichvu "
		)->queryScalar() + 1;
		$this->data["row"] = $row;
		$this->data['page']= $page;	
		$this->render("form_edit",$this->data);
	}
	
	function actionEdit($dichvu_id = 0, $page = 1)
	{
		$this->action = "Cập nhật";
		$row = NULL;
		
		if ($dichvu_id == 0)
		{
			$this->redirect(array($this->controller_id.'/view',
				"parent_id"=>$parent_id, "page" => $page));
		}else{
			$row = dichvu::model()->findByPk($dichvu_id);
			$this->data['row'] = $row;
			$this->data["page"] = $page;
			$this->render("form_edit", $this->data);
		}
	}
	
	function actionSave($dichvu_id = 0, $page = 1)
	{
		$row = NULL;
		if ($dichvu_id == 0)
		{
			$row = new dichvu;
			$row->setAttributes($_POST, false);
			$row->alias = trim(strtolower(MyHelper::convert_no_sign($row->name)));
			$row->save();
		}else{
			$row = dichvu::model()->findByPk($dichvu_id);
			$row->setAttributes($_POST, false);
			$row->alias = trim(strtolower(MyHelper::convert_no_sign($row->name)));
			
			if ($row->id != Yii::app()->getRequest()->getPost("parent_id"))
				$row->save();
		}
		$this->redirect(array($this->controller_id.'/view', "page" => $page));
	}
	
	function actionEnable($dichvu_id, $page = 1)
	{
		$row = dichvu::model()->findByPk($dichvu_id);
		$row->enable = 1 - $row->enable;
		$row->save();
		$this->redirect(array($this->controller_id.'/view', "page" => $page));
	}
	
	function actionSet_order($page = 1)
	{
		//Tao thu tu mac dinh
		$rows = dichvu::model()->findAll();
		foreach($rows as $row)
		{
			$row->order = $row->id;
			$row->save();
		}
		$this->redirect(array($this->controller_id.'/view',  "page"=>$page));	
	}
	
	function actionSave_order($page = 0)
	{
		$orders = Yii::app()->getRequest()->getPost("order");
		
		foreach($orders as $id=>$value)
		{
			$row = dichvu::model()->findByPk($id);
			$row->order = $value;
			$row->save();
		}
		
		//print_r($orders);
		$this->redirect(array($this->controller_id.'/view', "page"=>$page));		
	}
	
	function actionDelete($dichvu_id, $page = 1)
	{
		$row = dichvu::model()->findByPk($dichvu_id);
	
		$row->delete();
		
		$rows = NULL;
		
		$rows = dichvu::model()->findAll(array(
			"offset" => ($page - 1) * $this->website["beperpage"],
			"limit" => $this->website["beperpage"]
		));	
		
		if (count($rows) == 0)
		{
			if ($page > 1)
				$page --;
		}
			
		$this->redirect(array($this->controller_id.'/view', "page" => $page));
	}
	
	function actionDelete_multi($parent_id = 0, $page = 1)
	{
		//var_dump(Yii::app()->getRequest()->getPost("cib"));
		
		$id_array = Yii::app()->getRequest()->getPost("cib");
		if (!is_null($id_array))
		{
			foreach($id_array as $id=>$value)
			{
				$row = dichvu::model()->findByPk($id);
				
				$count_product = product::model()->count(array(
					"condition" => "trash = 0 AND dichvu_id = " . $row->id
				));
				
				if ($count_product == 0)
					$row->delete();
			}
		}
		
		$rows = NULL;
		
	
		$rows = dichvu::model()->findAll(array(
			"offset" => ($page - 1) * $this->website["beperpage"],
			"limit" => $this->website["beperpage"]
		));	
		
		$this->redirect(array($this->controller_id.'/view', "page" => $page));
	}
}